package com.pl.action;  

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.code.kaptcha.Producer;
import com.pl.bean.ManageUserBean;
import com.pl.bean.ResultData;
import com.pl.service.ManagePageService;
import com.pl.service.ManageUserService;
import com.pl.util.ResultDataUtil;

/**
 * 
 */
/**
 * class name:LoginAction <BR>
 * class description: please write your description <BR>
 * Remark: <BR>
 * @version 1.00 2019年8月5日
 * @author JFTT)wangjian
 */
@Controller
@RequestMapping(value="login")
public class LoginAction {

	private static Logger log = LogManager.getLogger();
	
	@Resource(name="manageUserService")
	private ManageUserService manageUserService;
	
	@Resource(name="managePageService")
	private ManagePageService managePageService;
	
	@Autowired
    private Producer captchaProducer;
	
	/**
	 * 登录页面
	 */
	@RequestMapping(value="loginPage")
	public String loginPage(HttpServletRequest request){
		
		return "login/login";
	}
	
	/**
	 * 登陆
	 */
	@RequestMapping(value="login")
	@ResponseBody
	public ResultData<String> login(HttpServletRequest request, String userName, String password, String imageCode){
		
		String result = manageUserService.validateLogin(request, userName, password, imageCode);
		if(result == null) {
			//验证成功
			ManageUserBean userBean = manageUserService.getManageUserByUserName(userName);
			manageUserService.afterSuccessLogin(request, userBean);
			
			return ResultDataUtil.ok();
		}else {
			return ResultDataUtil.fail(result);
		}
	}
	
}
